import { http } from "@/utils/http";

// 通用响应接口
export interface ApiResponse<T = any> {
  code: number;
  data?: T;
  message: string;
}

// 装修验收记录接口
export interface DecorationInspection {
  id?: number;
  tenantId?: number;
  decorationId: number;
  inspectionDate?: string;
  inspector: string;
  inspectionResult: string;
  inspectionNotes: string;
  inspectionPhotosUrl?: string;
  isDeleted?: number;
  createTime?: string;
  updateTime?: string;
}

// 分页响应接口
interface PageResponse<T> {
  total: number;
  list: T[];
}

// 装修管理查询DTO
export interface DecorationManagementQueryDto {
  page: number;
  limit: number;
  keyword?: string; // 用于搜索姓名/房号/手机号
  tenantId?: number;
  cellId?: string;
  houseNumber?: string;
  applicant?: string;
  applicantPhone?: string;
  status?: string;
}

// 关键词查询DTO
export interface DecorationQueryDto {
  keyword?: string; // 用于搜索姓名/房号/手机号等
}

// 装修管理列表VO
export interface DecorationManagementListVo {
  id: number;
  cellId: string;
  cellName: string;
  houseNumber: string;
  decorationCompany: string;
  applicant: string;
  applicationDate: string;
  decorationDeposit: number;
  status: string;
  decorationContent: string;
  approver: string;
  isDeleted: number;
  tenantId: number;
  applicantPhone: string;
  constructionUnit: string;
  responsiblePerson: string;
  responsiblePhone: string;
  constructionStartTime: string;
  constructionEndTime: string;
  decorationDrawingsUrl: string;
  remark: string;
  totalFee: number;
  paymentStatus: number;
  rejectReason: string;
  createTime: string;
  updateTime: string;
}

// 装修详情DTO
export interface DecorationDetailDto {
  decorationManagement: DecorationManagementListVo;
  inspection?: DecorationInspection;
  fees?: any[];
}

// 获取装修管理列表
export const getDecorationManagementList = (queryDto: DecorationManagementQueryDto) => {
  return http.request<ApiResponse<PageResponse<DecorationManagementListVo>>>('post', '/decorationManagement/list', {
    data: queryDto
  });
};

// 获取装修详情（包含验收信息和费用信息）
export const getDecorationManagementById = (id: number) => {
  return http.request<ApiResponse<DecorationDetailDto>>('get', `/decorationManagement/detail-with-info/${id}`);
};

// 添加装修管理
export const addDecorationManagement = (data: any) => {
  return http.request<ApiResponse>('post', '/decorationManagement/add-with-charges', {
    data
  });
};

// 更新装修管理
export const updateDecorationManagement = (data: any) => {
  return http.request<ApiResponse>('post', '/decorationManagement/update', {
    data
  });
};

// 删除装修管理
export const deleteDecorationManagement = (id: number) => {
  return http.request<ApiResponse>('post', `/decorationManagement/delete/${id}`);
};

// 审核装修
export const approveDecoration = (id: number, status: string, approver: string) => {
  return http.request<ApiResponse>('post', `/decorationManagement/approve/${id}`, {
    params: { status, approver }
  });
};

// 验收装修
export const inspectDecoration = (id: number, inspector: string, inspectionResult: string, inspectionNotes: string, inspectionPhotosUrl?: string) => {
  return http.request<ApiResponse>('post', `/decorationManagement/inspect/${id}`, {
    params: { inspector, inspectionResult, inspectionNotes, inspectionPhotosUrl }
  });
};

// 批量删除装修信息
export const batchDeleteDecoration = (ids: number[]) => {
  return http.request<ApiResponse>('post', '/decorationManagement/batch-delete', {
    data: ids
  });
};

// 带费用的装修申请
export const applyDecorationWithFees = (applicationDto: any) => {
  return http.request<ApiResponse>('post', '/decorationManagement/apply-with-fees', {
    data: applicationDto
  });
};

// 更新支付状态
export const updatePaymentStatus = (decorationId: number, paymentStatus: number) => {
  return http.request<ApiResponse>('post', '/decorationManagement/update-payment-status', {
    params: { decorationId, paymentStatus }
  });
};

// 计算装修费用
export const calculateDecorationFee = (cellId: number, chargeStandardIds: number[], quantities: number[]) => {
  return http.request<ApiResponse>('post', '/decorationManagement/calculate-fee', {
    params: { cellId, chargeStandardIds, quantities }
  });
};

// 获取装修收费明细
export const getDecorationFees = (decorationId: number) => {
  return http.request<ApiResponse<any[]>>('get', `/decorationManagement/fees/detail/${decorationId}`);
};

// 根据关键词查询装修信息
export const findDecorationByKeyword = (queryDto: DecorationQueryDto) => {
  return http.request<ApiResponse<DecorationManagementListVo[]>>('post', '/decorationManagement/findByKeyword', {
    data: queryDto
  });
};